package com.example.demo.server;

import com.example.demo.dao.User;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * Created on 2019/9/22.
 *
 * @author Grak
 * @since 1.0
 */
@Service
public class RedisService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public boolean setUserBystringRedisTemplate(User user){
        ValueOperations ops=stringRedisTemplate.opsForValue();
        ops.set(user.getNickname(), JSONObject.fromObject(user).toString());
        return true;
    }

    public String getUserBystringRedisTemplate(String name){
        ValueOperations ops=stringRedisTemplate.opsForValue();
        return JSONObject.fromObject(ops.get(name)).toString();
    }

    public boolean setString(String key,String value){
        ValueOperations ops=stringRedisTemplate.opsForValue();
        ops.set(key,value);
        return true;
    }

    public String getString(String key){
        ValueOperations ops=stringRedisTemplate.opsForValue();
        return (String)ops.get(key);
    }

}
